package com.samphanie.auiu.auth.controller;

import com.samphanie.auiu.auth.domain.dto.Oauth2TokenDto;
import com.samphanie.auiu.common.constants.AuiuConstants;
import com.samphanie.auiu.common.utils.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.security.Principal;
import java.util.Map;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Api(tags = "认证中心登录认证")
@RequiredArgsConstructor
@RestController
@RequestMapping("/oauth")
public class AuthController {

    private final TokenEndpoint tokenEndpoint;

    @ApiOperation("用户登录Get")
    @GetMapping(value = "/token")
    public ApiResponse<Oauth2TokenDto> getAccessToken(@ApiIgnore Principal principal, @ApiIgnore @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();

        Oauth2TokenDto oauth2TokenDto = Oauth2TokenDto.builder()
                .token(oAuth2AccessToken.getValue())
                .refreshToken(oAuth2AccessToken.getRefreshToken().getValue())
                .expiresIn(oAuth2AccessToken.getExpiresIn())
                .tokenHead(AuiuConstants.JWT_TOKEN_PREFIX)
                .build();

        return ApiResponse.success(oauth2TokenDto);
    }

    @ApiOperation("用户登录Post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "grant_type", value = "授权模式", paramType = "query", dataTypeClass = String.class,required = true),
            @ApiImplicitParam(name = "client_id", value = "Oauth2客户端ID", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "client_secret", value = "Oauth2客户端秘钥", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "refresh_token", value = "刷新token", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "username", value = "登录用户名", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "登录密码", paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "scope", value = "使用范围", paramType = "query", dataTypeClass = String.class, required = true)
    })
    @RequestMapping(value = "/token", method = RequestMethod.POST)
    public ApiResponse<Oauth2TokenDto> postAccessToken(@ApiIgnore Principal principal, @ApiIgnore @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();

        Oauth2TokenDto oauth2TokenDto = Oauth2TokenDto.builder()
                .token(oAuth2AccessToken.getValue())
                .refreshToken(oAuth2AccessToken.getRefreshToken().getValue())
                .expiresIn(oAuth2AccessToken.getExpiresIn())
                .tokenHead(AuiuConstants.JWT_TOKEN_PREFIX)
                .build();

        return ApiResponse.success(oauth2TokenDto);
    }

}
